/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved
*
*  This file is part of the GroupMe! Project. You can utilize
*  software of the GroupMe! project under the terms of the 
*  GNU Lesser General Public License as published by the
*  Free Software Foundation in version 2.1.
* 
*  The GNU Lesser General Public License can be found at:
*  http://www.gnu.org/licenses/lgpl.html
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.export.rdf;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import java.util.Collection;
import java.util.Iterator;
import net.groupme.export.*;
import net.groupme.export.constants.General;
import net.groupme.export.constants.UserConstants;
import net.groupme.model.Group;
import net.groupme.model.MODEL_CONSTANTS;
import net.groupme.model.User;

/**
 * This class provides...
 *
 * @author Daniel Plappert
 * @version 0.1
 */
public class UserExport implements IRdfDataExport, IUserExport {
    
    private Collection<User> data;
    
    /**
     * Creates a new instance of UserExport
     */
    public UserExport() {
    }
    
    /**
     * 
     *
     * @param Collection<User>
     */
    public void setData(Collection<User> data) {
        this.data = data;
    }    
    
    
    /**
     *
     *
     * @param Model
     */
    public void exportData(Model model, boolean exportAll) {
       
        Resource tempRes = null;
        Literal tempLit = null;
        
        for(User tempUser : data) {
            
            tempRes = model.createResource(tempUser.getURI().toString());
            tempRes.addProperty(RDF.type, General.userBaseUri);
            
            // username attribute
            tempLit = model.createTypedLiteral(tempUser.getUserName());
            tempRes.addProperty(UserConstants.usernameProperty, tempLit);
            
            // firstname attribute
            tempLit = model.createTypedLiteral(tempUser.getFirstName());
            tempRes.addProperty(UserConstants.firstnameProperty, tempLit);
            
            // lastname attribute
            tempLit = model.createTypedLiteral(tempUser.getLastName());
            tempRes.addProperty(UserConstants.lastnameProperty, tempLit);
            
            // email attribute
            tempLit = model.createTypedLiteral(tempUser.getEmail());
            tempRes.addProperty(UserConstants.emailProperty, tempLit);
            
            // foaf URL attribute
            // TODO: woher?
            //tempLit = model.createTypedLiteral()
           
            // groups
            exportGroups(tempRes, UserConstants.isOwnerOfProperty, tempUser.getGroups(MODEL_CONSTANTS.ROLE_OWNER));
            exportGroups(tempRes, UserConstants.isModeratorOfProperty, tempUser.getGroups(MODEL_CONSTANTS.ROLE_MODERATOR));
            exportGroups(tempRes, UserConstants.isSubscriberOfProperty, tempUser.getGroups(MODEL_CONSTANTS.ROLE_SUBSCRIBER));
            
        }
        
    }  
    
    
    /**
     *
     *
     * @param Resource
     * @param Property
     * @param Collection<Group>
     */
    private void exportGroups(Resource tempRes, Property property, Collection<Group> groups) {
        
        for(Group group : groups) {
            tempRes.addProperty(property, group.getURI().toString());
        }
        
    }
    
    
}
